﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.IO;
using System.Collections.Generic;

using HHo_Negocio;
using HHo_Model;

public partial class Consultas_wfRankTrab : System.Web.UI.Page
{
    GestorOrigen oGesOri;
    GestorConsultas oGesCons;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ListItem li = new ListItem();
            li.Text = StringConstantes.DDL_BIND_PARAM_TEXT_TODOS;
            li.Value = StringConstantes.DDL_BIND_PARAM_VAL_TODOS;


            oGesOri = new GestorOrigen();
            ddl_Origen.DataSource = oGesOri.ListarOrigen();
            ddl_Origen.DataTextField = "Codigo";
            ddl_Origen.DataValueField = "Id";
            ddl_Origen.DataBind();
            ddl_Origen.Items.Add(li);
            ddl_Origen.SelectedValue = li.Value;
        }
    }


    private string PrepareHTMLEtiqExcel()
    {
        string strLabel;
        strLabel = "<table border='1'><tr><td>Fecha de Inicio</td><td>";
        strLabel += txtFecIni.Text;
        strLabel += "</td><td>Fecha de Fin</td><td>";
        strLabel += txtFecFin.Text;
        strLabel += "</td></tr><tr><td>Avion</td><td>";
        strLabel += txtMatAvi.Text;
        strLabel += "</td><td>Origen</td><td>";
        strLabel += ddl_Origen.Items[ddl_Origen.SelectedIndex].Text;
        strLabel += "</td></tr></table>";

        return strLabel;
    }

    protected void btnToExcel_Click(object sender, ImageClickEventArgs e)
    {
        ExportToExcel();
    }

    private void ExportToExcel()
    {
        GridView gv;
        gv = gvResult;
        gv.AllowPaging = false;
        gv.DataSource = null;
        gv.DataBind();

        gv.DataSource = CargaData();
        gv.DataBind();
        
        string atachado = "attachment; filename=Ranking.xls";
        Response.ClearContent();
        Response.Clear();
        Response.AddHeader("content-disposition", atachado);

        //Se supone debe abrir sin mostrar la ventanita de guardar
        //Response.Cache.SetCacheability(HttpCacheability.NoCache);

        Response.ContentType = "application/vnd.ms-excel";
        Response.Charset = "";
        this.EnableViewState = false;

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        HtmlForm frm = new HtmlForm();
        gv.Parent.Controls.Add(frm);        
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(gv);

        sw.Write(PrepareHTMLEtiqExcel());
        frm.RenderControl(htw);
        //hidDataFiltro.RenderControl(htw);
        //gvResult.RenderControl(htw);        
        Response.Write(sw.ToString());
        Response.End();
    }
    protected void imgConsultar_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            gvResult.DataSource = CargaData();
            gvResult.DataBind();
        }
        catch
        {
            lblMensaje.Text = "ERROR: No se ha podido consultar, revise los datos ingresados";
        }        
    }

    private List<E_ConsultaRankingTrab> CargaData()
    {
        DateTime DTFecIni, DTFecFin;
        List<E_ConsultaRankingTrab> lst = null;
        string MatAvion = "";
        int IdOrigen = -99;
        try
        {
            if (txtMatAvi.Text.Trim().Length > 0)
                MatAvion = HH_Toolkit.AsegurarFormatoMatricula(txtMatAvi.Text);

            if (ddl_Origen.SelectedValue != StringConstantes.DDL_BIND_PARAM_VAL_TODOS)
                IdOrigen = Int32.Parse(ddl_Origen.SelectedValue);

            DTFecIni = Convert.ToDateTime(txtFecIni.Text);
            DTFecFin = Convert.ToDateTime(txtFecFin.Text);
            oGesCons = new GestorConsultas();
            lst = oGesCons.ConsultaRankinTrab(DTFecIni, DTFecFin, MatAvion,IdOrigen );
        }
        catch (Exception ex)
        {
            lblMensaje.Text = "ERROR del sistema:<br/>" + ex.Message;
        }        
        return lst;
    }
    protected void gvResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvResult.PageIndex = e.NewPageIndex;
        gvResult.DataSource = CargaData();
        gvResult.DataBind();
    }
}
